package com.simons.common.http.connPool;

import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import lombok.extern.log4j.Log4j2;

/**
 * 

 * @Description:TODO

 * @author:jsm
	
 * @time:2017年9月15日 下午2:10:41
 */

@Log4j2
public class HttpClientConnManager{
	
	 private static PoolingHttpClientConnectionManager clientConnectionManager=null;
	    
	 public static RequestConfig config = RequestConfig.custom()
			 											.setCookieSpec(CookieSpecs.STANDARD_STRICT)
			 											.setSocketTimeout(5000)  
			 									        .setConnectTimeout(5000)  
			 									        .setConnectionRequestTimeout(5000)
			 											.build();
	    
	 private static Integer MAXTOTAL = 50;
	 
	 private static Integer MAXPERROUTE = 25;
	 
    /**
     * 创建httpclient连接池并初始化
     */
    static{
    	 log.info("------------HttpClientConnManager init start----------------");
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .build();
        clientConnectionManager =new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        clientConnectionManager.setMaxTotal(MAXTOTAL);
        clientConnectionManager.setDefaultMaxPerRoute(MAXPERROUTE);
        log.info("------------HttpClientConnManager init end----------------");
    }
     
     
    public PoolingHttpClientConnectionManager getClientConnectionManager(){
    	return clientConnectionManager;
    }
    
    public RequestConfig getRequestConfig(){
    	return config;
    }

}
